/*
第一次修改优化了随机数,代码见 https://www.bilibili.com/read/cv11974105
第二次修改修复了输出错误，代码已删除。
第三次修改做到了下落式，代码已删除。
第四次修改修复了刷新屏幕时闪烁以及时间和规定时间内击中数量显示不正确的Bug。
Version4.1修复了4号位空白问题
Version5目前属于测试版
Version4.1.1 大量双押等你挑战!
类似于别踩白块禅模式,20秒电脑版。
推荐编译器(编辑器):Dev-C++(下载页面：https://lanzoux.com/imXG3gpfjej)
Dev-C++备用下载页面: https://lanzoui.com/imXG3gpfjej
Dev-C++适用于Windows7-64Bit以上。若您是XP系统请使用VC6.0以上。
*/
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <thread>
using namespace std;
char c;
int Miss = 0, Tap = 0, b[8], j[8];

char a[4] = {'d', 'f', 'j', 'k'}, k;
int d = 0;
time_t start = time(0);

void gotoxy(short x, short y) {
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

SYSTEMTIME time1;
namespace gamestart {
	ofstream fout("游玩数据.txt", ios::app);
	void gameinit() {
		GetLocalTime(&time1);
		for (int i = 1; i <= 7; i++) {
			b[i] = rand() % 4;
			j[i] = rand() % 4;
		}
	}
	void gamert() {
		while (time(0) - start <= 20) {
			for (int i = 1; i <= 7; i++) {
				if (b[i] == 0 && j[i] == 0)
					cout << "■ □ □ □\n";
				else if (b[i] == 0 && j[i] == 1)
					cout << "■ ■ □ □\n";
				else if (b[i] == 0 && j[i] == 2)
					cout << "■ □ ■ □\n";
				else if (b[i] == 0 && j[i] == 3)
					cout << "■ □ □ ■\n";
				else if (b[i] == 1 && j[i] == 0)
					cout << "■ ■ □ □\n";
				else if (b[i] == 1 && j[i] == 1)
					cout << "□ ■ □ □\n";
				else if (b[i] == 1 && j[i] == 2)
					cout << "□ ■ ■ □\n";
				else if (b[i] == 1 && j[i] == 3)
					cout << "□ ■ □ ■\n";
				else if (b[i] == 2 && j[i] == 0)
					cout << "■ □ ■ □\n";
				else if (b[i] == 2 && j[i] == 1)
					cout << "□ ■ ■ □\n";
				else if (b[i] == 2 && j[i] == 2)
					cout << "□ □ ■ □\n";
				else if (b[i] == 2 && j[i] == 3)
					cout << "□ □ ■ ■\n";
				else if (b[i] == 3 && j[i] == 0)
					cout << "■ □ □ ■\n";
				else if (b[i] == 3 && j[i] == 1)
					cout << "□ ■ □ ■\n";
				else if (b[i] == 3 && j[i] == 2)
					cout << "□ □ ■ ■\n";
				else if (b[i] == 3 && j[i] == 3)
					cout << "□ □ □ ■\n";
			}
			if (b[7] != j[7]) {
				k = getch();
				c = getch();
			} else {
				c = getch();
				k = c;
			}
			if ((c == a[b[7]] || k == a[b[7]]) && (c == a[j[7]] || k == a[j[7]])) {
				Tap++;
				b[7] = b[6];
				b[6] = b[5];
				b[5] = b[4];
				b[4] = b[3];
				b[3] = b[2];
				b[2] = b[1];
				b[1] = rand() % 4;
				j[7] = j[6];
				j[6] = j[5];
				j[5] = j[4];
				j[4] = j[3];
				j[3] = j[2];
				j[2] = j[1];
				j[1] = rand() % 4;
				gotoxy(0, 0);
			} else {
				d = time(0) - start;
				system("cls");
				break;
			}
			gotoxy(0, 0);
		}
		system("cls");
		if (d == 0) {
			cout << "击中:" << Tap - 1 << " 耗时约20\"";
			fout << time1.wYear << "年" << time1.wMonth << "月" << time1.wDay << "日 " << time1.wHour << ":" << time1.wMinute << ":"
			     << time1.wSecond << " 击中:" << Tap - 1 << " 耗时约20\"\n";
		} else {
			cout << "您击错了。\n击中:" << Tap << " 耗时约" << d << '\"';
			fout << time1.wYear << "年" << time1.wMonth << "月" << time1.wDay << "日 " << time1.wHour << ":" << time1.wMinute << ":"
			     << time1.wSecond << " 击中:" << Tap << " 耗时约" << d << "\"\n";
		}
	}
}

int main() {
	cout << "请按照屏幕上的字符\"■\"按下键盘按键(1:D,2:f,3:J,4:K)\n按任意键开始游戏\n";
	getch();
	system("cls");
	srand(time(0));
	while (1) {
		gamestart::gameinit();
		gamestart::gamert();
		cout << "\n按r继续游戏，按其他键结束。\n";
		char z;
		z = getch();
		system("cls");
		if (z != 'r' && z != 'R')
			break;
	}
	cout << "游玩数据已经保存到本目录下的 \"游玩数据.txt\"。";
}